package org.bao.pet;

import java.io.File;

import org.bao.pet.config.ConfigFileReader;
import org.bao.pet.tools.Resizer;
import org.bao.pet.tools.Watermarker;
import org.bao.pet.utils.FileSystem;
import org.bao.pet.utils.PlatformInfo;

public class ControlCentre {
	public static final String TOOL_TYPE_RESIZER = "resizer";
	public static final String TOOL_TYPE_WATERMARKER = "watermarker";
	
	
	public void init(){
		ConfigFileReader cfr = new ConfigFileReader("./pet.properties");
		
		PlatformInfo pfi = PlatformInfo.getInstance();
		
		String separator = ((cfr.get(ConfigFileReader.CONFIG_PLATFORM).equalsIgnoreCase(ConfigFileReader.CONFIG_PLATFORM_WINDOWS)) ? PlatformInfo.SEPARATOR_WINDOWS : PlatformInfo.SEPARATOR_LINUX);
		
		pfi.set(PlatformInfo.INFO_TYPE_SEPARATOR, separator);
		
		//get workspace.
		pfi.set(PlatformInfo.INFO_TYPE_WORKSPACE,cfr.get(ConfigFileReader.CONFIG_WORKSPACE));
		
		//get watermark file path and register it as one member of PlatformInfo.
		pfi.set(PlatformInfo.INFO_TYPE_WATERMARK, cfr.get(ConfigFileReader.CONFIG_WATERMARK_PATH));
	}
	
	/**
	 * dispatch different kinds of tools
	 * @param type
	 * @param para0 file path or directory.
	 * @param para1 If type == WATERMARKER,para1 will be ignored.
	 * @param para2 If type == WATERMARKER,para2 will be ignored.If para2 == 0,then para1 means a factor.
	 */
	public void dispatch(String type,String para0,int para1,int para2){
		if(type == ControlCentre.TOOL_TYPE_RESIZER){
			Resizer re = new Resizer();
			
			if(para2 == 0){
				re.config(para0,para1);
			}
			else{
				re.config(para0,para1,para2);
			}
			
			re.zoom();
		}
		else
		if(type == ControlCentre.TOOL_TYPE_WATERMARKER){
			Watermarker wm = new Watermarker();
			
			wm.config(para0);
			wm.add();
		}
	}
	
	public void run(){
		this.init();
		
		File dir = new File(PlatformInfo.getInstance().get(PlatformInfo.INFO_TYPE_WORKSPACE));
		
		if(dir.exists() && dir.isDirectory()){
			for(int i = 0; i < dir.listFiles().length; i++){
				if(dir.listFiles()[i].isFile() && FileSystem.isValidImageFile(dir.listFiles()[i].getAbsolutePath())){
					this.dispatch(ControlCentre.TOOL_TYPE_RESIZER, dir.listFiles()[i].getAbsolutePath(), 1024, 768);
				}
			}
		}
		
		String watermark = PlatformInfo.getInstance().get(PlatformInfo.INFO_TYPE_WATERMARK);
		
		if(watermark.length() > 0){
			File NDir = new File(PlatformInfo.getInstance().get(PlatformInfo.INFO_TYPE_WORKSPACE) + PlatformInfo.getInstance().get(PlatformInfo.INFO_TYPE_SEPARATOR) + FileSystem.NEW_DIRECTORY_NAME_DEFAULT);
			
			if(NDir.exists() && NDir.isDirectory()){
				for(int i = 0; i < NDir.listFiles().length; i++){
					if(NDir.listFiles()[i].isFile() && FileSystem.isValidImageFile(NDir.listFiles()[i].getAbsolutePath())){
						this.dispatch(ControlCentre.TOOL_TYPE_WATERMARKER, NDir.listFiles()[i].getAbsolutePath(), 0, 0);
					}
				}
			}
		}
		
	}
	
	
	public static void main(String[] args){
		ControlCentre cc = new ControlCentre();
		
		cc.run();
	}                      
}
